package com.alibobo.utils.json;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.alibobo.utils.file.FileUtils;

public class JsonUtils {

	public static JSONArray loadJSONArrayFromClassPath(String n) {
		InputStream in = JsonUtils.class.getClassLoader()
				.getResourceAsStream(n);
		JSONArray jo = null;
		if (in != null) {
			String content = FileUtils.readInputStreamAsString(in);
			try {
				jo = new JSONArray(content);
			} catch (Exception e) {

			} finally {
				try {
					in.close();
				} catch (Exception e2) {
				}
			}
		}
		return jo;

	}

	public static JSONObject loadJSONObjectFromClassPath(String n) {
		InputStream in = JsonUtils.class.getClassLoader()
				.getResourceAsStream(n);
		JSONObject jo = null;
		if (in != null) {
			String content = FileUtils.readInputStreamAsString(in);
			try {
				jo = new JSONObject(content);
			} catch (Exception e) {

			} finally {
				try {
					in.close();
				} catch (Exception e2) {
				}
			}
		}
		return jo;
	}

	public static List<String> readStringArrayFrom(JSONArray ja) {
		List<String> rr = new ArrayList<String>();
		try {

			int sz = ja.length();
			for (int i = 0; i < sz; i++) {
				rr.add(ja.getString(i));
			}
		} catch (Exception e) {
		}
		return rr;
	}
}
